Adding authorship information to MediaWiki pages

The perhaps most visible example of search engines starting to look for semantic data is the author information in Google search results, such as shown in the image above. Here is how we made pages like that one (a ginger bread recipe) on Säsongsmat.nu appear with author information in Google’s search results.

First of all: The whole point with using a wiki is to have many people, or personas, editing pages collaboratively. And Google won’t be able to handle multiple authors, as far as I know. Still, there might be cases where showing a single author is relevant. In our case we have a namespace for recipes, where the creator of an article is by default the only one allowed to make changes, and other people can only fork the recipe to start creating their own version.

So we have pages that make good candidates for author markup, now what? Säsongsmat.nu is heavily based on Semantic MediaWiki, so that’s what we will be using to retrieve and print the author(s). It would, however, not be difficult to write an extension for the same purpose without using Semantic MediaWiki.

Get the authors

To automatically get the list of authors of an article we use an extension called Semantic Extra Special Properties (first version written by us, heavily improved by others), that automatically adds semantic data about things like authors, number of revisions, etc.

If you plan to add authorship information to just an handful of articles, you could skip this step and create the links by hand.

Print out the authors

The way to tell Google that a certain name is refering to the author of an article is to add the attribute rel=author to the link’s html code. This is not something that MediaWiki will allow us to do by default, so we needed to write yet another extension: LinkAttributes. This extension extends MediaWiki’s link syntax so that we can write things like [[User:Leo|Leo|rel=author]]. Changing the wiki syntax is not always a good idea, so you might want to adjust the extension for your own needs (e.g. by creating a parser function instead). This works for us, however.

Next we need to create a template to be included in pages where we want authorship information visible. If you are using the page creator property of SemanticExtraSpecialProperties, you just need to query SMW for it, and put the result in a link. If you want all page contributors, you will need to use the Arrays extensions, like we do in our template:

{{#arraydefine:authors|{{#show:{{FULLPAGENAME}}|?Page contributor|link=none}}}}{{#if:{{#arrayprint:authors}}|
==Authors==
{{#arrayprint:authors||@@@@|{{Author-row|@@@@}}}}
}}}}

And in the template author-row:

[[{{{1|User:Example}}}|{{#replace:{{{1|User:Example}}}|{{ns:User}}:|}}|rel=author]]

Which will produce something like this:

[[Användare:Leo|Leo|rel=author]]
Which will in its turn produce a html output like this:
<a href="http://xn--ssongsmat-v2a.nu/ssm/Användare:Leo" rel="author">Leo</a>

Update: Claus points out in the comments that the #replace parser function is part of the ParserFunctions extension, that needs to be installed. Furthermore $wgPFEnableStringFunctions must be set to true in LocalSettings.php.

Note that it’s also possible to have MediaWiki print out the N last contributors to a page in the footer by setting $wgMaxCredits to N. You could use that output to add this data, by writing your own extension hooking into.

Connect user pages with Google+

To make authors appear in Google search results they need to have a Google+ page. And Google will need to be able to connect the author to that Google+ page. We do that by adding a field to the template that makes up user pages, making room for an optional Google+ profile url, and by, once again, adding an attribute to that url when printing it: rel=me. And while we are on it we add the same attribute to Facebook, blog and Twitter links as well.

Social media links in the user page form
As we are using SemanticForms to edit pages in the wiki, and have set the whole user page namespace to use the same form by default, it’s easy to add and remove templates from each user’s page.

Finally, the user needs to add our site to the list of contributing sites in Google+ (profile/edit)

It can take some time for results to appear in Google search results.

Summary

  • You need MediaWiki with the SemanticMediaWiki, ParserFunctions, SemanticExtraSpecialProperties and LinkAttributes extensions (disclaimer: I wrote the last two, and they most certainly contain some examples of bad coding in them). Make sure $wgPFEnableStringFunctions = true; in LocalSettings.php.
  • You probably want the Array and SemanticForms extensions as well
  • Add a template to query Semantic MediaWiki for author(s), and put the result in a link to the author user page with the rel=author attribute
  • Make sure users use a user page template with a field for Google+ user page url. If you are using SemanticForms, this will be a piece of cake.
  • Tell users that they need to add your site to the list of sites they are contributing to in Google+, in order to be credited in the Google search results.

2 reaktion på “Adding authorship information to MediaWiki pages

  1. Hi, this article has been very helpful in setting authorship up on my single-user wiki. I wish more Semantic MediaWiki stuff was documented this clearly 🙂 Your SESP and LinksAttributes extensions work just fine on MediaWiki 1.21.2.

    I would like to clarify that in order to use the mentioned ”#replace” function, the ParserFunctions extension needs to be activated and (I assume) ”$wgPFEnableStringFunctions = true;” needs to be set in LocalSettings.php. Being a new MediaWiki user this tripped me up a bit.

    Thanks, Claus

Kommentera